package com.example.project4;


import java.util.ArrayList;
import java.util.HashMap;

import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.app.ListActivity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListAdapter;
import android.widget.SimpleAdapter;

/** TESTING EXPORT AND EXPORTS **/

public class MyMusic extends ListActivity {

	// Songs list
    public ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
    static final String EXTRA_MESSAGE = "com.example.Project4.MESSAGE";   
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_music);
		updateList();
	}
	
	/**
	 * Bind to the service when application starts
	 */
    @Override
    protected void onStart() {
        super.onStart();
        bindService(new Intent(this, ConnectionService.class), mConnection,
            Context.BIND_AUTO_CREATE);
    }

    /**
     * Unbind from Service when application stops
     */
    @Override
    protected void onStop() {
        super.onStop();
        if (mBound) {
            unbindService(mConnection);
            mBound = false;
        }
    }
    
	/**
	 * Add menu with list, diff, pull, leave and cap to the activity layout
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.options_menu, menu);
		return true;
	}
    
	/**
	 * Updates the songs in the client playlist
	 */
	private void updateList() {
		ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>();      
        SongsManager plm = new SongsManager();
        // get all songs from sdcard
        this.songsList = plm.getPlayList();
        // looping through playlist
        for (int i = 0; i < songsList.size(); i++) {
            // creating new HashMap
            HashMap<String, String> song = songsList.get(i);
            // adding HashList to ArrayList
            songsListData.add(song);
        }
        // Adding menuItems to ListView
        ListAdapter adapter = new SimpleAdapter(this, songsListData,R.layout.playlist_item, new String[] { "songTitle" }, new int[] { R.id.songTitle });
        setListAdapter(adapter);		
	}
	
	/**
	 * Sends a message to the ConnectionService indicating what 
	 * menu option was selected
	 */
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        if (!mBound) 
        	return false;
        // Create and send a message to the service, using a supported 'what' value
        Message msg;
        
        switch (item.getItemId()){
	        case R.id.menu_list:
	        	msg = Message.obtain(null, ConnectionService.LIST, 0, 0);
	        	break;
	        case R.id.menu_diff:
	        	msg = Message.obtain(null, ConnectionService.DIFF, 0, 0);
	        	break;
	        case R.id.menu_pull:
	        	msg = Message.obtain(null, ConnectionService.LEAVE, 0, 0);
	        	break;
	        case R.id.menu_cap:
	        	msg = Message.obtain(null, ConnectionService.CAP, 0, 0);
	        	break;
	        default: 
	        	msg = Message.obtain(null, ConnectionService.CONNECT, 0, 0);
	        	break;
        }
        
        try {
        	/** send message to Service **/
            mService.send(msg);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
        return true;
	}
	 
	 /** Needed to communicate with the service. */
	 Messenger mService = null;

	 /** Flag indicating whether we have called bind on the service. */
	 boolean mBound;

	 /**
	  ** Class for interacting with the main interface of the service.
	  */
	  private ServiceConnection mConnection = new ServiceConnection() {
		  public void onServiceConnected(ComponentName className, IBinder service) {
		      mService = new Messenger(service);
		      mBound = true;
		  }
	
		  public void onServiceDisconnected(ComponentName className) {
			  mService = null;
			  mBound = false;
		  }
	  };
}
